<?php
include_once ("model/accountModel.php");
include_once ("_include/common.php");

class accountController {
    public $model;

    public function __construct() {
        $this -> model = new accountModel();
    }

    public function invoke() {
        if ($_REQUEST["do"]) {
            switch ($_REQUEST["do"]) {
                case 'login' :
                    $this -> login();
                    break;

                case 'logout' :
                    $this -> logout();
                    break;

                case 'regist' :
                    $this -> regist();
                    break;

                default :
                    $this -> viewLoginPage();
                    break;
            }

        } else {
            $this -> viewLoginPage();
        }

    }

    public function login() {
        session_start();
        $_SESSION["username"] = NULL;
        $_SESSION["role"] = NULL;

        $user = $_REQUEST["username"];
        $password = $_REQUEST["password"];

        if (!$user) {
            $this -> viewLoginPage('Please fill username and password');
            return;
        }

        $account = $this -> model -> selectAccount($user);
        if ($password != $account[0]["Password"]) {
            $this -> viewLoginPage('Wrong usename or password');
            return;
        }

        $_SESSION["username"] = $account[0]["Account"];
        $_SESSION["role"] = $account[0]["role"];
        header("Location: " . $_SESSION["returnurl"]);
    }

    public function logout() {
        session_start();
        $_SESSION["username"] = NULL;
        $_SESSION["role"] = NULL;
        header("Location: index.php");
    }

    public function viewLoginPage($loginmessage = "", $regmessage = "") {
        session_start();
        $username = $_SESSION['username'];

        $account = NULL;
        if ($account = $this -> model -> selectAccount($username)) {
            $Name = $account[0]['Name'];
            $Password = $account[0]['Password'];            
            $Phone = $account[0]['Phone'];
            $Email = $account[0]['Email'];
            $Birthday = $account[0]['Birthday'];
            $Address = $account[0]['Address'];
            $About = $account[0]['About'];
        }

        include 'view/accountDetail.php';
    }

    public function regist() {
        $username = $_REQUEST['username'];
        $Name = $_REQUEST['Name'];
        $Password = $_REQUEST['Password'];
        $Password2 = $_REQUEST['Password2'];
        $Phone = $_REQUEST['Phone'];
        $Email = $_REQUEST['Email'];
        $Birthday = $_REQUEST['Birthday'];
        $Address = $_REQUEST['Address'];
        $About = $_REQUEST['About'];
        $result = $this -> model -> addAccount($username, $Password, $Name, $Email, $Birthday, "User", $Address, $Phone, $About);

        if ($result) {
            $message = "Regist account : $username is successful";
            include 'view/info.php';
        } else {
            $regmessage = "Regist account : $username is fail";
            include 'view/accountDetail.php';
        }

    }

}
